package coml.jiyun.hp.ershi.fragment;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import coml.jiyun.hp.ershi.DbUtil;
import coml.jiyun.hp.ershi.R;
import coml.jiyun.hp.ershi.adapter.RlvHomeAdapter;
import coml.jiyun.hp.ershi.bean.WanBean;
import coml.jiyun.hp.ershi.bean.WanBeanTwo;
import coml.jiyun.hp.ershi.model.ImpWanModel;
import coml.jiyun.hp.ershi.presenter.ImpWanPresenter;
import coml.jiyun.hp.ershi.view.WanView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements WanView {


    private RecyclerView mRlv;
    private ArrayList<WanBean.DataBean.DatasBean> datasBeans;
    private RlvHomeAdapter rlvHomeAdapter;
    private int mpoi;

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_home, container, false);
        initView(inflate);
        initData();
        registerForContextMenu(mRlv);
        return inflate;
    }

    private void initData() {
        new ImpWanPresenter(new ImpWanModel(),this).getData();
    }

    private void initView(View inflate) {
        mRlv = inflate.findViewById(R.id.home_rlv);

        datasBeans = new ArrayList<>();
        rlvHomeAdapter = new RlvHomeAdapter(getActivity(), datasBeans);
        mRlv.setAdapter(rlvHomeAdapter);
        mRlv.setLayoutManager(new LinearLayoutManager(getActivity()));

        rlvHomeAdapter.setOnItemClick(new RlvHomeAdapter.onItemClick() {
            @Override
            public void dj(int i) {
                mpoi = i;
                initCollect();
            }
        });

        rlvHomeAdapter.setOnItemLongClick(new RlvHomeAdapter.onItemLongClick() {
            @Override
            public void ca(View view, int i) {

            }
        });
    }

    private void initCollect() {
        WanBeanTwo wanBeanTwo = new WanBeanTwo();
        wanBeanTwo.setTitle(datasBeans.get(mpoi).getTitle());
        DbUtil.dbUtil().insert(wanBeanTwo);
        Toast.makeText(getActivity(), "收藏成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCreateContextMenu(@NonNull ContextMenu menu, @NonNull View v, @Nullable ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(1,1,1,"删除");
        menu.add(1,2,1,"修改");
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case 1:

                break;

            case 2:
        }
        return super.onContextItemSelected(item);
    }


    @Override
    public void onSuccess(WanBean wanBean) {
        datasBeans.addAll(wanBean.getData().getDatas());
        rlvHomeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFail(String str) {

    }


}